package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Room;
import cn.wolfcode.mapper.RoomMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.RoomService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    /**
     * 添加或修改
     * @param room
     */
    @Override
    @Transactional
    public void saveOrUpdate(Room room) {
        if (room.getId() != null) {
            roomMapper.updateByPrimaryKey(room);
        }else {
            roomMapper.insert(room);
        }
    }

    /**
     * 根据宿舍Id删除
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Long id) {
        //删除宿舍自身数据
        roomMapper.deleteByPrimaryKey(id);
        //删除中间表数据

    }

    /**
     *通过Id查询宿舍信息
     * @param id
     * @return
     */
    @Override
    public Room selRoomById(Long id) {
        return roomMapper.selectByPrimaryKey(id);
    }

    /**
     * 展示所有宿舍信息
     * @return
     */
    @Override
    public List<Room> selAll() {
        return roomMapper.selectAll();
    }

    //分页
    public PageInfo<Room> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Room> rooms = roomMapper.selForList(qo);
        return new PageInfo<>(rooms);
    }

}
